﻿using Lt.Managers;
using RayDen.Library.Components;
using RayDen.Library.Components.Color;
using RayDen.Library.Core.Primitives;
using RayDen.Library.Entity.Scene;

namespace Lt.Engine.Data
{
    public class EmissionProfile
    {
        public string Name, SpdFile;
        public ITexture EmissionTexture;
        
        public bool InvertTexture;
        public float[] EmissionSpectra;
        public bool SpectralRendering;

        internal int textureID;

        public IRadianceEvaluation Evaluate(float u, float v)
        {
            if (EmissionTexture != null)
            {
                RgbSpectrum tex;
                TextureManager.Instance.Sample(textureID, u, v, out tex);
                if (InvertTexture)
                {
                    tex = RgbSpectrum.Unit - tex;
                }
                return ColorManager.Instance.Convert(ref tex, true);
            }

            return ColorManager.Instance.Create(EmissionSpectra, true);
        }

        public EmissionProfile(string spdFile)
        {
            EmissionSpectra = SPD_Data.FromSampled(SPD_Data.FromFile(spdFile),
                                             SpectrumType.Illuminant).ToArray();
            this.SpdFile = spdFile;
        }


        public EmissionProfile()
        {
            EmissionSpectra = new float[0];
        }

    }
}